//
// domain="[
//     ('applicability', '=', 'taxes'),
//     '|', ('country_id', '=', parent.tax_country_id),
//     ('country_id', '=', False),
// ]"

// const last_date = env.date_tools.increase(new Date(), -1)
//
// domain value 计算
//
// editform:   ['field_name', '=', {field_other_field_name:{} } ]
// editform:   ['field_name', '=', {session_current_company_id:{} } ]

// searchview: ['field_name', 'ilike', {self:{} } ]
// searchview: ['field_name', '>', { datetime_lastdate:{} } ]

// =================================
// domain: [['user_id', '=', { session_uid: {} }]]
// ---------------------------------------
// datetime_lastdate:
//  const last_date = env.date_tools.increase(new Date(), -1)
// [
// '|',
// ('date_end', '=', False),
// ('date_end', '&gt;=',
//         (context_today() - datetime.timedelta(days=1)).strftime('%Y-%m-%d'))
// ]
// ---------------------------------------
// ('scheduled_date', '&lt;', time.strftime('%Y-%m-%d %H:%M:%S')),
// ---------------------------------------
// domain="[('inventory_date', '&lt;=', context_today().strftime('%Y-%m-%d'))]
// ---------------------------------------
//    ['company_id', '=', { session_current_company_id: {} }]

function _domain_maker(raw, kws = {}) {
  //   console.log('_domain_maker:', raw, kws)

  const { env, meta, record, context, self } = kws

  function make_val(val) {
    if ('self' in val) {
      return self
    }

    // if ('env' in val) {
    //   const es = val.env.split('.')
    //   let res = env
    //   for (const el of es) {
    //     // console.log(res, el)
    //     res = res[el]
    //   }

    //   // console.log(es, res)
    //   return res
    // }

    const [ntype, ...ns] = Object.keys(val)[0].split('_')
    const fname = ns.join('_')
    if (!fname) {
      console.log('get_domain1,', val)
      throw 'error ' + 'get_domain1'
    }

    if (ntype === 'unknow') {
      console.log('get_domain1', val)
      throw 'error'
    } else if (ntype === 'context') {
      console.log('get_domain1 context', val)
      throw 'error'
    } else if (ntype === 'datetime') {
      console.log('get_domain1 datetime', val)
      throw 'error'
    } else if (ntype === 'session') {
      const { env } = kws
      if (env.session[fname]) {
        return env.session[fname]
      } else {
        console.log('get_domain1', val)
        throw 'error'
      }
    } else if (ntype === 'field') {
      const [fn1, ...fns2] = fname.split('_')
      const fn2 = fns2.join('_')
      if (fn1 === 'parent') {
        const val2 = record[fn1][fn2]
        return val2
      } else {
        const val2 = record[fname]
        if (fname === 'id' && !val2) {
          return 0
        }
        return val2
      }
    } else {
      console.log('get_domain1', val)
      throw 'error'
    }
  }

  if (typeof raw === 'string') {
    console.log('domain is string. not an array', meta.name, raw)
    throw 'domain is string. not an array: ' + meta.name + ', ' + raw
  }

  if (typeof raw === 'function') {
    console.log('domain is function. not an array')
    throw 'domain is function. not an array: ' + meta.name
  }

  const domain2 = raw.map(item => {
    if (Array.isArray(item)) {
      const [fn, op, val] = item
      if (!val) {
        return item
      } else if (['number', 'boolean', 'string'].includes(typeof val)) {
        return item
      } else if (Array.isArray(val)) {
        const val2 = val.map(it => {
          if (['number', 'string', 'boolean'].includes(typeof it)) {
            return it
          } else if (typeof it === 'object') {
            return make_val(it)
          } else {
            throw 'error'
          }
        })

        return [fn, op, val2]
      } else if (typeof val !== 'object') {
        return item
      } else {
        const val2 = make_val(val)
        return [fn, op, val2]
      }
    } else {
      return item
    }
  })

  return domain2
}

export const domain_helper = {
  domain_maker(logic, kws = {}) {
    // console.log('domain_maker:', logic, kws)
    const res = _domain_maker(logic, { ...kws })
    //   // console.log('domain_maker:', res)
    return res
  }
}
